package org.eclipse.assemblyformatter;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.assemblyformatter.ir.Formatter;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;

/**
 * <h3>Deploying a plugin</h3>
 * http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/org.
 * eclipse.pde.doc.user/guide/pde_deploy.htm <br />
 * http://www.ibm.com/developerworks/library/os-ecplug/ <br />
 * 
 * <p>
 * To control what plugins are loaded for debugging, open
 * "Run Configurations...".
 * </p>
 * 
 * <p>
 * To make Eclipse open assembly files:
 * <ol>
 * <li>Go to Window -> Preferences -> General -> Editors -> File Associations</li>
 * <li>Add file type "*.s"</li>
 * <li>Associate "Text Editor" for "*.s"</li>
 * </ol>
 * </p>
 * 
 * <p>
 * To install this plugin:
 * <ol>
 * <li>Download the plugin .zip file.</li>
 * <li>Copy the .jar file from the archive to the Eclipse directory <code>dropins\<code>.</li>
 * <li>Restart Eclipse.</li>
 * </ol>
 * </p>
 * 
 */
public class FormatAction implements IWorkbenchWindowActionDelegate {

	IWorkbenchWindow activeWindow = null;

	/**
	 * Run the action.
	 */
	public void run(IAction proxyAction) {
		// proxyAction has UI information from manifest file (ignored)
		Shell shell = activeWindow.getShell();

		// MessageDialog.openInformation(shell, "Assembly Formatter",
		// "Press \"Format As Assembly\"...");

		IWorkbenchPage page = activeWindow.getActivePage();
		IEditorPart part = page.getActiveEditor();
		if (part instanceof AbstractTextEditor) {
			// MessageDialog.openInformation(shell, "Editor part",
			// "Found active editor");
			ITextEditor editor = (ITextEditor) part;
			IDocumentProvider dp = editor.getDocumentProvider();
			IDocument doc = dp.getDocument(editor.getEditorInput());
			Formatter formatter = new Formatter(doc);

			try {
				formatter.replaceAnyTab(8);
			} catch (BadLocationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

			final String directory = "E:\\assembly-formatter\\debug\\";
			formatter.tokenize();
			try {
				formatter.writeSectionList(directory
						+ "section-list tokenize().xml");
			} catch (ParserConfigurationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (IOException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (TransformerException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

			try {
				formatter.parse();
			} catch (BadLocationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

			try {
				formatter.writeSectionList(directory
						+ "section-list parse().xml");
			} catch (ParserConfigurationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (IOException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (TransformerException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

			try {
				formatter.rewrite();
			} catch (BadLocationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

			try {
				formatter.writeSectionList(directory
						+ "section-list format().xml");
			} catch (ParserConfigurationException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (IOException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			} catch (TransformerException e) {
				MessageDialog.openError(shell, e.getClass().getCanonicalName(),
						e.getMessage());
			}

		} else {
			MessageDialog.openError(shell, "Editor part", "No active editor");
		}
	}

	// IActionDelegate method
	public void selectionChanged(IAction proxyAction, ISelection selection) {
		// do nothing, action is not dependent on the selection
	}

	// IWorkbenchWindowActionDelegate method
	public void init(IWorkbenchWindow window) {
		activeWindow = window;
	}

	// IWorkbenchWindowActionDelegate method
	public void dispose() {
		// nothing to do
	}

}
